package com.tgy.JUC.VolatileDemo;

import java.util.concurrent.TimeUnit;

/***
 * @ClassName: JMMTest
 * @Description: 测试volatile
 * @Auther: 送你一块二向箔
 * @Date: 15:35 2022/3/15
 * @version : V1.0
 */
public class JMMTest {
    //private static int i = 0;
    private static volatile int i = 0;//加上volatile 保证可见性
    public static void main(String[] args) {
        run1();
    }
    public static void run1(){
        new Thread(()->{//程序一直运行,对主内存的变化不可见
            while (i == 0){}
        }).start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        i = 2;
        System.out.println(i);
    }
}
